from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage, InvalidPage
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render

from ScenicSpot.models import TourName, Lable, Address, Img
from User.models import TourUser, ReadTour, LikeTour, com


def index(request):
    '''
    首页
    :param request:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    lable = Lable.objects.all()
    # 标签
    new = TourName.objects.all().order_by('-created')[0:3]
    new1 = TourName.objects.all().order_by('-created')[0:1]
    new2 = TourName.objects.all().order_by('-created')[1:3]
    # 最新发布1~3
    address = Address.objects.all()
    # 获取地点标签
    img = Img.objects.all().order_by('-id')[0:1]
    img2 = Img.objects.all().order_by('-id')[1:3]

    host = TourName.objects.all().order_by('-readnum')[0:3]

    return render(request, 'tour/index.html', locals())


def search(request):
    '''
    搜索
    :param request:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    img = Img.objects.all().order_by('-id')[0:1]
    img2 = Img.objects.all().order_by('-id')[1:3]
    title = request.GET['title']
    tour = TourName.objects.filter(title__contains=title).order_by('-id')
    paginator = Paginator(tour, 6)
    try:
        page_number = request.GET.get('page', '1')
        page = paginator.page(page_number)
    except (PageNotAnInteger, EmptyPage, InvalidPage):
        # 如果出现上述异常，默认展示第1页
        page = paginator.page(1)
    return render(request, 'tour/search.html', locals())


def lable(request,xq):
    '''
    标签分类
    :param request:
    :param xq:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    tour = TourName.objects.filter(lable_id=xq)
    paginator = Paginator(tour, 6)
    try:
        page_number = request.GET.get('page', '1')
        page = paginator.page(page_number)
    except (PageNotAnInteger, EmptyPage, InvalidPage):
        # 如果出现上述异常，默认展示第1页
        page = paginator.page(1)
    return render(request, 'tour/lable.html', locals())


def address(request,xq):
    '''
    地址分类
    :param request:
    :param xq:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    tour = TourName.objects.filter(address_id=xq)
    paginator = Paginator(tour, 6)
    try:
        page_number = request.GET.get('page', '1')
        page = paginator.page(page_number)
    except (PageNotAnInteger, EmptyPage, InvalidPage):
        # 如果出现上述异常，默认展示第1页
        page = paginator.page(1)
    return render(request, 'tour/address.html', locals())


def read(request,xq):
    '''
    详情页
    :param request:
    :param xq:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)


    comment = com.objects.filter(about_id=xq)
    for c in comment:
        print(c.user.uphoto)
        usercommern = c.user
        print(usercommern.uphoto)


    text = TourName.objects.get(id=xq)
    new = TourName.objects.filter(address=text.address).order_by('-likenum')[0:3]
    new1 = TourName.objects.filter(address=text.address).order_by('-likenum')[0:1]
    new2 = TourName.objects.filter(address=text.address).order_by('-likenum')[1:3]
    lable = Lable.objects.all()
    # 标签
    address = Address.objects.all()
    # 获取地点标签
    a = 'block'
    b = 'block'
    if session:
        if ReadTour.objects.filter(uphone=session,tid=xq):
            # 阅读数
            if LikeTour.objects.filter(uphone=session,tid=xq):
                a = 'none'
            readnum = ReadTour.objects.filter(tid=xq).count()
            return render(request, 'tour/text.html', locals())
        else:
            try:
                ReadTour.objects.create(
                    uphone=session,
                    tid=TourName.objects.get(id=xq),
                )
                # 阅读数
                readnum = ReadTour.objects.filter(tid=xq).count()
                a = TourName.objects.get(id=xq)
                a.readnum = readnum
                a.save(update_fields=['readnum'])
                return HttpResponseRedirect('/read/%s' % xq)
            except Exception as e:
                return HttpResponseRedirect('/read/%s'%xq)
    else:
        return render(request, 'tour/text.html', locals())

def like(request,xq):
    '''
    点赞
    :param request:
    :param xq:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    if session:
        if LikeTour.objects.filter(uphone=session,tid=xq):
            # 阅读数
            # likenum = LikeTour.objects.filter(tid=xq).count()
            return HttpResponseRedirect('/read/%s'%xq)
        else:
            try:
                LikeTour.objects.create(
                    uphone=session,
                    tid=TourName.objects.get(id=xq),
                )
                likenum = LikeTour.objects.filter(tid=xq).count()
                a = TourName.objects.get(id=xq)
                a.likenum = likenum
                a.save(update_fields=['likenum'])
                return HttpResponseRedirect('/read/%s'%xq)
            except Exception as e:
                return HttpResponseRedirect('/read/%s'%xq)
    else:
        return HttpResponse('你还没有登录')


def host(request):
    '''
    查看更多热门文章
    :param request:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    img = Img.objects.all().order_by('-id')[0:1]
    img2 = Img.objects.all().order_by('-id')[1:3]
    host = TourName.objects.all().order_by('-readnum')
    paginator = Paginator(host, 6)
    try:
        page_number = request.GET.get('page', '1')
        page = paginator.page(page_number)
    except (PageNotAnInteger, EmptyPage, InvalidPage):
        # 如果出现上述异常，默认展示第1页
        page = paginator.page(1)
    return render(request, 'tour/host.html', locals())


def new(request):
    '''
    查看更多最新发布
    :param request:
    :return:
    '''
    session = request.session.get('username')
    usermessage = TourUser.objects.filter(uphone=session)
    # 获取用户信息
    img = Img.objects.all().order_by('-id')[0:1]
    img2 = Img.objects.all().order_by('-id')[1:3]
    new = TourName.objects.all().order_by('-created')
    paginator = Paginator(new, 6)
    try:
        page_number = request.GET.get('page', '1')
        page = paginator.page(page_number)
    except (PageNotAnInteger, EmptyPage, InvalidPage):
        # 如果出现上述异常，默认展示第1页
        page = paginator.page(1)
    return render(request, 'tour/host.html', locals())